package com.happycat.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.happycat.config.HappyCatSecurity;
import com.happycat.mobile.model.AccountRoleMapping;
import com.happycat.mobile.model.UserAccount;
import com.happycat.mobile.service.AccountRoleMappingService;
import com.happycat.mobile.service.AccountRoleService;
import com.happycat.mobile.service.UserAccountService;
import com.happycat.utils.AesUtils;

@Component
public class UserDetailServiceImpl implements UserDetailsService{
	
	
	@Autowired
	private UserAccountService userAccountService;
	
	@Autowired
	private AccountRoleMappingService accountRoleMappingService;
	
	@Autowired
	private AccountRoleService accountRoleService;
	@Autowired
	private PasswordEncoder passwordEncoder;
	@Autowired
	private HappyCatSecurity happyCatSecurity;
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		
		if(StringUtils.isEmpty(username)) {
            throw new UsernameNotFoundException("UserDetailsService没有接收到用户账号");
        } 
        /**
         * 根据用户名查找用户信息
         */
    	UserAccount userAccountParam = new UserAccount();
    	userAccountParam.setAccountName(username);
    	Wrapper<UserAccount>  accountQuery = new QueryWrapper<>(userAccountParam);
    	UserAccount userAccount = userAccountService.getOne(accountQuery);
        if(userAccount == null) {
        	//注册并发送短信验证码
        	userAccountService.register(userAccountParam);        	
        }
        AccountRoleMapping accountRoleMapping = new AccountRoleMapping();
        accountRoleMapping.setAccountId(userAccount.getId());
        Wrapper<AccountRoleMapping> accountRoleQueryWrapper = new QueryWrapper<>(accountRoleMapping);
        List<AccountRoleMapping> lists = accountRoleMappingService.list(accountRoleQueryWrapper); 
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        for (AccountRoleMapping role : lists) {
            //封装用户信息和角色信息到SecurityContextHolder全局缓存中
            grantedAuthorities.add(new SimpleGrantedAuthority(accountRoleService.getById(role.getRoleId()).getRoleName()));
        }
        /**
         * 创建一个用于认证的用户对象并返回，包括：用户名，密码，角色
         */
        return new User(userAccount.getAccountName(),passwordEncoder.encode(AesUtils.decrypt(userAccount.getPassword(), happyCatSecurity.getAesKey())) , grantedAuthorities);
    
	}

}
